package com.pzb.web.controller.goods;

import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.core.domain.entity.GdGoodsClass;
import com.pzb.common.core.page.TableDataInfo;
import com.pzb.goods.domain.GdGoods;
import com.pzb.goods.service.IGdGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品管理Controller
 * 
 * @author Mr_Wu
 * @date 2021-11-30
 */
@RestController
@RequestMapping("/goods")
public class GdGoodsController extends BaseController
{
    @Autowired
    private IGdGoodsService gdGoodsService;

    /**
     * 查询商品分类列表
     */
    @GetMapping("/getGoodsClass")
    public AjaxResult getGoodsClass(@RequestParam("extAppid") String extAppid)
    {
        List<GdGoodsClass> list = gdGoodsService.selectGdGoodsClassList(extAppid);
        return AjaxResult.success(list);
    }

    /**
     * 查询商品列表
     */
    @GetMapping("/getGoodsList")
    public TableDataInfo getGoodsList(@RequestParam("sortType") String sortType,@RequestParam("sortPrice") Integer sortPrice,@RequestParam("pageNum") Integer pageNum,@RequestParam("extAppid") String extAppid, @RequestParam(name = "classId",required = false) Long classId, @RequestParam(name = "name",required = false) String name)
    {
        String order = "t1.id";
        if("all".equals(sortType)){
            order = "t1.id";
        }else if("sales".equals(sortType)){
            order = "t1.id desc";
        }else if("price".equals(sortType)){
            if(sortPrice == 0){
                order = "reference_price";
            }else {
                order = "reference_price desc";
            }
        }
        startPage();
        List<GdGoods> list = gdGoodsService.selectGdGoodsList(extAppid,classId,name,order);
        return getDataTable(list);
    }

    /**
     * 查询商品列表
     */
    @GetMapping("/getPanicList")
    public TableDataInfo getPanicList( @RequestParam(name = "panicId") Long panicId)
    {
        startPage();
        List<GdGoods> list = gdGoodsService.selectPanicGoodsList(panicId);
        return getDataTable(list);
    }


    /**
     * 查询商品详情
     */
    @GetMapping("/getGoodsDetail")
    public AjaxResult getGoodsDetail(@RequestParam(name = "goodsId") Long goodsId,@RequestParam("extAppid") String extAppid)
    {
        GdGoods goods = gdGoodsService.selectGdGoodsById(goodsId,extAppid,"");
        return AjaxResult.success(goods);
    }

}
